<template>
  <div>
    <!-- 顶部区域 -->
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <a
            :class="['mui-control-item',item.id === 0 ? 'mui-active' : '']"
            href="#item1mobile"
            data-wid="tab-top-subpage-1.html"
            v-for="item in categoryList"
            :key="item.id"
            @click="getPhotoListByCateId(item.id)"
          >{{ item.title }}</a>
        </div>
      </div>
    </div>
    <!-- 图片列表区域 -->
    <ul class="photo-list">
      <router-link v-for="item in list" :key="item.id" :to="'/home/photoinfo/'+item.id" tag='li'>
        <img v-lazy="item.img_url">
        <div class="info">
          <h1 class="title">{{item.title}}</h1>
          <div class="body">{{item.zhaiyao}}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
// 导入mui的js文件
import mui from "../../lib/mui/js/mui.min.js";
// 初始化滑动控件

export default {
  data() {
    return {
      categoryList: [],
      list: [] // 图片列表的数组
    };
  },
  mounted() {
    // 当组件的dom结构被渲染好并放到页面中，会执行这个钩子函数
    // 如果要操作元素，最好在 mounted 中，这时候dom元素是最新的
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
    });
  },
  methods: {
    getCategory() {
      this.$http.get("api/getimgcategory").then(res => {
        if (res.body.status === 0) {
          this.categoryList = res.body.message;
          this.categoryList.unshift({ title: "全部", id: 0 });
        } else {
          Toast("获取数据失败");
        }
      });
    },
    getPhotoListByCateId(cateId) {
      // 根据 分类id 获取图片列表
      this.$http.get("api/getimages/" + cateId).then(res => {
        if (res.body.status === 0) {
          this.list = res.body.message;
        }
      });
    }
  },

  created() {
    this.getCategory();
    // 页面加载完成默认获取全部的列表
    this.getPhotoListByCateId(0);
  }
};
</script>

<style lang='less' scoped>
* {
  touch-action: pan-y;
}

.photo-list {
  padding: 0;
  margin: 10px;
  li {
    background-color: #ccc;
    list-style-type: none;
    text-align: center;
    margin-bottom: 10px;
    box-shadow: 0 0 10px rgb(175, 175, 175);
    position: relative;
    img {
      width: 100%;
      vertical-align: middle;
      // display: block;
    }
    img[lazy="loading"] {
      width: 40px;
      height: 300px;
      margin: auto;
    }
    .info {
      padding: 0 8px;
      color: white;
      text-align: center;
      position: absolute;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.3);
      max-height: 84px;
      .title {
        font-size: 14px;
      }
      .body {
        font-size: 13px;
      }
    }
  }
}
</style>
